package com.slg.android.manyanime.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.BitmapUtils;
import com.slg.android.manyanime.R;
import com.slg.android.manyanime.bean.CartoonBean;
import com.slg.android.manyanime.utils.DataStore;

import java.util.List;

/**
 * Created by Thinkpad on 2015/3/11.
 */
public class CartoonHotAdapter extends BaseAdapter {

    public CartoonHotAdapter(Context context,List<CartoonBean> data){
        this.context=context;
        this.data=data;
    }


    private Context context;

    private List<CartoonBean> data;

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = null;
        if (convertView != null) {
            view = convertView;
        } else {
            view = inflater.inflate(R.layout.item_cartoon_hot, parent, false);
        }
        ViewHolder viewHolder = (ViewHolder) view.getTag();
        if (viewHolder == null) {
            viewHolder = new ViewHolder();
            viewHolder.img = (ImageView) view.findViewById(R.id.cartoon_hot_img);
            viewHolder.name = (TextView) view.findViewById(R.id.cartoon_hot_name);
            viewHolder.lastup = (TextView) view.findViewById(R.id.cartoon_hot_lastup);
            view.setTag(viewHolder);
        }
        //设置加载中和失败的图片
        BitmapUtils bitmapUtils = DataStore.getBitmapUtils();
        bitmapUtils.configDefaultLoadFailedImage(R.drawable.cartoon_hot_loading);
        bitmapUtils.configDefaultLoadingImage(R.drawable.cartoon_hot_loading);
        CartoonBean cartoonBean = data.get(position);
        if (cartoonBean != null) {
            viewHolder.name.setText(cartoonBean.getCartName());
            viewHolder.lastup.setText(cartoonBean.getLastup());
            bitmapUtils.display(viewHolder.img, cartoonBean.getPic());

        }
        return view;
    }

    public class ViewHolder {
        public ImageView img;
        public TextView name;
        public TextView lastup;


    }


}
